//
//  YQLoginPageView.m
//  MeiMeng
//
//  Created by weixb on 2017/3/22.
//  Copyright © 2017年 weixb. All rights reserved.
//

#import "YQLoginPageView.h"
#import "YQInputMobileView.h"
#import "YQInputCodeView.h"
#import <YQTools.h>
#import <Masonry.h>

@interface YQLoginPageView ()<YQLoginPageDelegate>

@property (nonatomic, strong) UIScrollView *scrollView;
@property (nonatomic, strong) YQInputMobileView *loginView;
@property (nonatomic, strong) YQInputCodeView *inputCodeView;

@end

@implementation YQLoginPageView

#pragma mark - public methods
+ (instancetype)manager {
    static id instance = nil;
    // 使用GCD
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        // 保证代码执行一次
        instance = [[[self class] alloc]init];
    });
    return instance;
}
/* 显示登录窗口 */
- (void)showPageView {
    UIWindow *keyWindow = [UIApplication sharedApplication].keyWindow;
    [keyWindow addSubview:self.scrollView];
    CGRect frame = [UIScreen mainScreen].bounds;
    UIView *leftView = [[UIView alloc] initWithFrame:frame];
    UIView *rightView = [[UIView alloc] initWithFrame:frame];
    rightView.x = SCREEN_WIDTH;
    [_scrollView addSubview:leftView];
    [_scrollView addSubview:rightView];
    
    [self.loginView showInView:leftView];
    [self.inputCodeView showInView:rightView];
    [_scrollView yq_whenTapped:^(UITapGestureRecognizer * _Nonnull tapGesture) {
        [[UIApplication sharedApplication].keyWindow endEditing:YES];
    }];
}

#pragma mark - Delegate
/* 每一个delegate都把对应的protocol名字带上,
 比如: UITableViewDelegate的方法集就老老实实写上#pragma mark - UITableViewDelegate
 */
#pragma mark - <YQLoginPageDelegate>
- (void)nextStepWithphoneNumber:(NSString *)phoneNumber {
    _inputCodeView.phoneNumber = phoneNumber;
    [_scrollView setContentOffset:CGPointMake(SCREEN_WIDTH, 0) animated:YES];
}
- (void)backToLoginView {
    [_scrollView setContentOffset:CGPointZero animated:YES];
}
- (void)successWithLogin {
    
}
- (void)dismiss {
    [_scrollView removeFromSuperview];
}
#pragma mark - event response
/* 所有button、gestureRecognizer的响应事件都放在这个区域里面 */

#pragma mark - private methods
/* 关于private methods，正常情况下ViewController里面不应该写,
 要么把它写成一个category，要么把他做成一个模块，哪怕这个模块只有一个函数也行。
 */

#pragma mark - getters and setters
- (UIScrollView *)scrollView {
    if (_scrollView == nil) {
        _scrollView = [[UIScrollView alloc] initWithFrame:[UIScreen mainScreen].bounds];
        _scrollView.contentOffset = CGPointZero;
        _scrollView.contentSize = CGSizeMake(2 * SCREEN_WIDTH, SCREENH_HEIGHT);
        _scrollView.bounces = NO;
        _scrollView.pagingEnabled = YES;
        
//        _scrollView.scrollEnabled = NO;
        _scrollView.backgroundColor = [UIColor yq_colorWithHex:000000 andAlpha:0.3];
    }
    return _scrollView;
}

- (YQInputMobileView *)loginView {
    if (_loginView == nil) {
        _loginView = [[YQInputMobileView alloc] init];
        _loginView.delegate = self;
    }
    return _loginView;
}
- (YQInputCodeView *)inputCodeView {
    if (_inputCodeView == nil) {
        _inputCodeView = [[YQInputCodeView alloc] init];
        _inputCodeView.delegate = self;
    }
    return _inputCodeView;
}

@end
